<?php

class App_Util_StringUtil {
	
	function __construct() {
	
	}
	
    /**
     * startsWith
     * Tests if a text starts with an given string.
     *
     * @param     string
     * @param     string
     * @return    bool
     */
    static public function startsWith($haystack, $needle){
        return strpos($haystack, $needle) === 0;
    }
	
	static public function quitar_acentos($cadena){
		$cadena = ereg_replace("[äáàâãª]","a",$cadena);
		$cadena = ereg_replace("[ÄÁÀÂÃÄ]","A",$cadena);
		$cadena = ereg_replace("[ÏÍÌÎ]","I",$cadena);
		$cadena = ereg_replace("[ïíìî]","i",$cadena);
		$cadena = ereg_replace("[ëéèê]","e",$cadena);
		$cadena = ereg_replace("[ËÉÈÊ]","E",$cadena);
		$cadena = ereg_replace("[öóòôõº]","o",$cadena);
		$cadena = ereg_replace("[ÖÓÒÔÕ]","O",$cadena);
		$cadena = ereg_replace("[üúùû]","u",$cadena);
		$cadena = ereg_replace("[ÜÚÙÛ]","U",$cadena);
		$cadena = str_replace("ç","c",$cadena);
		$cadena = str_replace("Ç","C",$cadena);
		$cadena = ereg_replace("[ñ]","n",$cadena);
		$cadena = ereg_replace("[Ñ]","N",$cadena);
		return $cadena;
	}
	
	function quitar_acentos_letras($cadena){
		$cadena = str_replace("&aacute;","á",$cadena);
		$cadena = str_replace("&eacute;","é",$cadena);
		$cadena = str_replace("&iacute;","í",$cadena);
		$cadena = str_replace("&oacute;","ó",$cadena);
		$cadena = str_replace("&uacute;","ú",$cadena);
		$cadena = str_replace("&ntilde;","ñ",$cadena);
		$cadena = str_replace("\""," ",$cadena);
		return $cadena;
	}
	
	function sql_ordering($filtros,$datos){
		/*
	 * Ordering
	 */
		if ( isset( $datos['iSortCol_0'] ) )
		{
			$sOrder = "ORDER BY  ";
			for ( $i=0 ; $i<intval( $datos['iSortingCols'] ) ; $i++ )
			{
				if ( $datos[ 'bSortable_'.intval($datos['iSortCol_'.$i]) ] == "true" )
				{
					$sOrder .= " ".$filtros[ intval( $datos['iSortCol_'.$i] ) ]." ".
						($datos['sSortDir_'.$i]==='asc' ? 'ASC' : 'DESC') .", ";
				}
			}
			
			$sOrder = substr_replace( $sOrder, "", -2 );
			if ( $sOrder == "ORDER BY" )
			{
				$sOrder = " ORDER BY {$filtros[0]} ASC ";
			}
		}
		return $sOrder;
	}
	
	function sql_search($aColumns,$datos){
		$sWhere = "";
		if ( isset($datos['sSearch']) && $datos['sSearch'] != "" )
		{
			$sWhere = " (";
			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			{
				if ( isset($datos['bSearchable_'.$i]) && $datos['bSearchable_'.$i] == "true")
				{
					if ($aColumns[$i][1]==1){
						$sWhere .= " UPPER(".$aColumns[$i][0].") LIKE '%". strtoupper($datos['sSearch']) ."%' OR ";
					}else{
						$sWhere .= " CAST(".$aColumns[$i][0]." AS TEXT) LIKE '%". $datos['sSearch'] ."%' OR ";
					}
				}
			}
			$sWhere = substr_replace( $sWhere, "", -3 );
			$sWhere .= ')';
		}
		return $sWhere;
	}
	
}

?>